"-------------------------------------------------------------------------------
" window
"-------------------------------------------------------------------------------
"

" Save & quit
noremap Q :q<CR>
" noremap <C-q> :qa<CR>
noremap S :w<CR>
noremap <silent> X s

" R in ranger will open ranger
noremap M :source $MYVIMRC<CR>

" Open the vimrc file anytime
noremap <LEADER>rc :e ~/.config/nvim/init.vim<CR>
noremap <LEADER>rv :e .nvimrc<CR>

" make Y to copy till the end of the line
nnoremap Y y$

" Copy to system clipboard & X11 剪贴板
" vnoremap Y "+y:let @*="<C-R>+"<CR>
vnoremap Y "+y:let @*=@+<CR>
" :let @+=@%

" :p理解为path, :h理解为head, :t理解为tail。可参考:help expand
" copy filename
noremap yfn :let @+=expand("%:t")<CR>:let @*=expand("%:t")<CR>
" 绝对路径
noremap yfp :let @+=expand("%:p")<CR>:let @*=expand("%:p")<CR>
" 绝对目录
" noremap yfd :let @+=expand("%:p:h")<CR>:let @*=expand("%:p:h")<CR>
" noremap yfs :let @+=expand("%")<CR>:let @*=expand("%")<CR>
" 相对路径
noremap yfr :let @+=substitute(substitute(expand("%"), ".*src/main/java", "classes", ""), ".java", ".class", "")<CR>:let @*=@+<CR>
" echo substitute(expand("%"), ".*src/main/java", "classes", "")
" 相对目录
noremap yfd :let @+=expand("%:p:h:t")<CR>:let @*=expand("%:p:h:t")<CR>

noremap cd :cd %:h<CR>


vnoremap <LEADER>t :s/ \+/ /g<CR>:nohlsearch<CR>

" Indentation
nnoremap < <<
nnoremap > >>

" Search
noremap <LEADER><CR> :nohlsearch<CR>


" Split window
nmap ss :split<Return><C-w>w
nmap sv :vsplit<Return><C-w>w
" split the screens to up (horizontal), down (horizontal), left (vertical), right (vertical)
noremap sk :set nosplitbelow<CR>:split<CR>:set splitbelow<CR>
noremap sj :set splitbelow<CR>:split<CR>
noremap sh :set nosplitright<CR>:vsplit<CR>:set splitright<CR>
noremap sl :set splitright<CR>:vsplit<CR>

" Resize splits with arrow keys
noremap <up> :res +5<CR>
noremap <down> :res -5<CR>
noremap <left> :vertical resize-5<CR>
noremap <right> :vertical resize+5<CR>

" Place the two screens up and down
noremap sH <C-w>t<C-w>K
" Place the two screens side by side
noremap sV <C-w>t<C-w>H

" Rotate screens
noremap srh <C-w>b<C-w>K
noremap srv <C-w>b<C-w>H

" Move window

" ===
" === Window management
" ===
" Use <space> + new arrow keys for moving the cursor around windows
noremap <LEADER>w <C-w>w
noremap <LEADER>k <C-w>k
noremap <LEADER>j <C-w>j
noremap <LEADER>h <C-w>h
noremap <LEADER>l <C-w>l
noremap qf <C-w>o

" ===
" === Tab management
" ===
" Create a new tab with tt
noremap tt :tabe<CR>
" Move around tabs with tj and tk
noremap tj :-tabnext<CR>
noremap tk :+tabnext<CR>
" Move the tabs with tmj and tmk
noremap tmj :-tabmove<CR>
noremap tmk :+tabmove<CR>

" ===
" === Markdown Settings
" ===
" Snippets
source ~/.config/nvim/md-snippets.vim
" auto spell
autocmd BufRead,BufNewFile *.md setlocal spell


" ===
" === Command Mode Cursor Movement
" ===
cnoremap <C-a> <Home>
cnoremap <C-e> <End>
cnoremap <C-p> <Up>
cnoremap <C-n> <Down>
cnoremap <C-b> <Left>
cnoremap <C-f> <Right>
cnoremap <M-b> <S-Left>
cnoremap <M-w> <S-Right>

" ===
" === Other useful stuff
" ===
" Open a new instance of st with the cwd
nnoremap \t :tabe<CR>:-tabmove<CR>:term sh -c 'st'<CR><C-\><C-N>:q<CR>

" Opening a terminal window
noremap <LEADER>/ :set splitbelow<CR>:split<CR>:res +10<CR>:term<CR>

" Press space twice to jump to the next '<++>' and edit it
noremap <LEADER><LEADER> <Esc>/<++><CR>:nohlsearch<CR>"_c4l

" Spelling Check with <space>sc
noremap <LEADER>sc :set spell!<CR>

" Auto change directory to current dir
autocmd BufEnter * silent! lcd %:p:h

" md filetype uses template
" autocmd BufNewFile *.md 0r /usr/share/vim/vimfiles/template.md
autocmd FileType vimwiki setlocal shiftwidth=3
autocmd FileType markdown setlocal shiftwidth=3

" set wrap
noremap <LEADER>sw :set wrap<CR>
noremap <LEADER>snw :set nowrap<CR>

" Call figlet
noremap tx :r !figlet 

" Compile function
" R in ranger will open ranger
noremap C :call CompileRunGcc()<CR>
func! CompileRunGcc()
  exec "w"
  if &filetype == 'c'
    exec "!g++ % -o %<"
    exec "!time ./%<"
  elseif &filetype == 'cpp'
    set splitbelow
    exec "!g++ -std=c++11 % -Wall -o %<"
    :sp
    :res -15
    :term ./%<
  elseif &filetype == 'java'
    set splitbelow
    :sp
    :res -5
    term javac % && time java %<
  elseif &filetype == 'sh'
    :!time bash %
  elseif &filetype == 'python'
    set splitbelow
    :sp
    :term python3 %
  elseif &filetype == 'html'
    silent! exec "!".g:mkdp_browser." % &"
  elseif &filetype == 'vimwiki'
    " exec "InstantMarkdownPreview"
    exec "MarkdownPreview"
  elseif &filetype == 'markdown'
    " exec "InstantMarkdownPreview"
    exec "MarkdownPreview"
  elseif &filetype == 'tex'
    silent! exec "VimtexStop"
    silent! exec "VimtexClean"
    silent! exec "VimtexReload"
    silent! exec "VimtexCompile"
  elseif &filetype == 'dart'
    exec "CocCommand flutter.run -d ".g:flutter_default_device." ".g:flutter_run_args
    silent! exec "CocCommand flutter.dev.openDevLog"
  elseif &filetype == 'javascript'
    set splitbelow
    :sp
    :term export DEBUG="INFO,ERROR,WARNING"; node --trace-warnings .
  elseif &filetype == 'go'
    set splitbelow
    :sp
    :term go run .
  endif
endfunc

" 插入模式移动光标
inoremap <C-h> <Left>
inoremap <C-j> <Down>
inoremap <C-k> <Up>
inoremap <C-l> <Right>
inoremap <C-d> <Delete>
" hh在我用的单词里出现的频率极低，其实感觉home用的没有end多，统一风格都用大写的吧
"inoremap HH <Home>
" 单词中包含ll的太多了，所以用大写LL
"inoremap LL <End>
" jk <Esc>
inoremap jk <Esc>
" 插入模式鼠标滚轮抵消,不然会出现滚动鼠标录入了一堆5j5k
inoremap 5k <Esc>
inoremap 5j <Esc>
inoremap 9<CR> <Esc>a
" 向下5行
noremap <C-j> 5j
" 向上5行
noremap <C-k> 5k

" Coc智能处理，使用IDEA Alt+Enter 同样按键
"noremap <M-Enter> :CocAction<CR>
inoremap <C-s> <ESC> :w<CR>
" 代码格式化
noremap <leader>f :Format<CR> 
noremap <leader>r :luafile ~/.wp/lua/run.lua<CR>

" 加/减数字1
nnoremap + <C-a>
nnoremap - <C-x>
"-------------------------------------------------------------------------------
" Buffers
"-------------------------------------------------------------------------------
" Open current directory
"nmap te :tabedit
"nmap <S-Tab> :tabprev<Return>
nmap <S-Tab> :bprev<Return>
"nmap <Tab> :tabnext<Return>
nmap <Tab> :bnext<Return>

noremap <LEADER>d :bd<CR>

" 窗口管理器
" invoke with '-'
" nmap  -  <Plug>(choosewin)
"nmap  sw  <Plug>(choosewin)
"nmap  <leader>w  <Plug>(choosewin)
"
autocmd FileType markdown nmap <buffer><silent> <leader>p :call mdip#MarkdownClipboardImage()<CR>
" there are some defaults for image directory and image name, you can change them
" let g:mdip_imgdir = 'img'
" let g:mdip_imgname = 'image'
